//
//  YuYueOrderCell.m
//  FD
//
//  Created by Yasin on 2018/10/23.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
// 2:49 -- 4:21
//纯代码cell布局。+ 请求数据 + 改bug
#define kFont14 14.0
#import "YuYueOrderCell.h"
@interface YuYueOrderCell()
/**背景 灰色背景*/
@property (nonatomic,  strong) UIView *bgView;
//上面的一块 ========
/**div1*/
@property (nonatomic,  strong) UIView *div1;
/**1.图标*/
@property (nonatomic,  strong) UIImageView *iconIv;
/**2.店面*/
@property (nonatomic,  strong) UILabel *dianNameLb;
/**3.创建时间时间 font:13.0*/
@property (nonatomic,  strong) UILabel *createTimeLb;





//中间的一块 ========
/**div2*/
@property (nonatomic,  strong) UIView *div2;
/**1.预定时间 font:13 添加到 diningTimeLbBgView 上*/
@property (nonatomic,  strong) UILabel *diningTimeLb;
/**2-12背景view*/
@property (nonatomic, strong) UILabel *diningTimeLbBgView;

/**1-1预定时间标题*/
@property (nonatomic,  strong) UILabel *diningTimeTitleLb;

/**2.预约号 font:30*/
@property (nonatomic,  strong) UILabel *yuyueNumLb;
/**2我的预约号标题*/
@property (nonatomic,  strong) UILabel *yuyueNumTitleLb;

/**3.人数标题*/
@property (nonatomic,  strong) UILabel *renshuTitleLb;
/**3.人数*/
@property (nonatomic,  strong) UILabel *renshuLb;



//下 ==========
/**div3*/
@property (nonatomic,  strong) UIView *div3;
/**预约点餐按钮 关闭响应者链，点击按钮就是点击cell*/
@property (nonatomic,  strong) UIButton *anNiuBtn;
@end

@implementation YuYueOrderCell

#pragma mark - 一 init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView {

    //取消cell样式
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.div1];
    [self.bgView addSubview:self.div2];
    [self.bgView addSubview:self.div3];
    
    //div1
    [self.div1 addSubview:self.iconIv];
    [self.div1 addSubview:self.dianNameLb];
    [self.div1 addSubview:self.createTimeLb];
    
    //div2
    [self.div2 addSubview:self.diningTimeTitleLb];
    [self.div2 addSubview:self.diningTimeLbBgView];
    [self.diningTimeLbBgView addSubview:self.diningTimeLb];
    
    [self.div2 addSubview:self.yuyueNumTitleLb];
    [self.div2 addSubview:self.yuyueNumLb];
    [self.div2 addSubview:self.renshuTitleLb];
    [self.div2 addSubview:self.renshuLb];
    
    //div3
    [self.div3 addSubview:self.anNiuBtn];
}

#pragma mark - 三 layout
//202
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    //div1 : 40
    [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(10); //10间隙
        make.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(40);
    }];
    //div2
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div1.mas_bottom).offset(1);
        make.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(100);
    }];
    //div3
    [self.div3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div2.mas_bottom).offset(1);
        make.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(50);
    }];
    
    //第一步的布局
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left).offset(10);
        make.centerY.equalTo(self.div1.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(40, 30));
    }];
    [self.dianNameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(150, 30));
        make.centerY.equalTo(self.div1.mas_centerY);
    }];
    [self.createTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.div1.mas_right).offset(-10);
        make.centerY.equalTo(self.div1.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150, 30));
    }];
    
    //第二部分的布局 div2: h = 100;
    //
    CGFloat w = self.contentView.bounds.size.width / 3;
    CGFloat h = 50;
    [self.diningTimeTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.div2);
        make.size.mas_equalTo(CGSizeMake(w, h));
    }];
    
    
    [self.diningTimeLbBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2);
        make.top.equalTo(self.diningTimeTitleLb.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(w, h));
    }];
    [self.diningTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.diningTimeLbBgView);
        make.width.mas_equalTo(100);
        make.centerX.equalTo(self.diningTimeLbBgView.mas_centerX);
    }];
    
    
    
    
    [self.yuyueNumTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.diningTimeLbBgView.mas_right);
        make.top.equalTo(self.div2.mas_top);
        make.size.mas_equalTo(CGSizeMake(w, h));
    }];
    [self.yuyueNumLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.diningTimeLbBgView.mas_right);
        make.top.equalTo(self.yuyueNumTitleLb.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(w, h));
    }];
    
    [self.renshuTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div2.mas_top);
        make.left.equalTo(self.yuyueNumLb.mas_right);
        make.size.mas_equalTo(CGSizeMake(w, h));
    }];
    [self.renshuLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.renshuTitleLb.mas_bottom);
        make.left.equalTo(self.yuyueNumLb.mas_right);
        make.size.mas_equalTo(CGSizeMake(w, h));
    }];
    
    //第三部分的布局
    [self.anNiuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.div3.mas_right).offset(-10);
//        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.height.mas_offset(30);
        make.width.mas_greaterThanOrEqualTo(80);
        make.centerY.equalTo(self.div3.mas_centerY);
    }];
}

#pragma mark - 二 getter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = rgba(245, 245, 245, 1.0);
    }
    return _bgView;
}

//!!!!:div1
- (UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
        _div1.backgroundColor = [UIColor whiteColor];
    }
    return _div1;
}
- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv = [UIImageView new];
    }
    return _iconIv;
}
- (UILabel *)dianNameLb {
    if (!_dianNameLb) {
        _dianNameLb = [UILabel new];
    }
    return _dianNameLb;
}
- (UILabel *)createTimeLb {
    if (!_createTimeLb) {
        _createTimeLb = [UILabel new];
//        _createTimeLb.font = [UIFont systemFontOfSize:14.0*kScaleFit];
        _createTimeLb.textColor = kLightGrayTextColor;
        _createTimeLb.font = [UIFont systemFontOfSize:13.0];
        _createTimeLb.textAlignment = NSTextAlignmentRight; //靠右
    }
    return _createTimeLb;
}




//!!!!:div2
//===========中-============
- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
        _div2.backgroundColor = [UIColor whiteColor];
    }
    return _div2;
}

- (UILabel *)diningTimeTitleLb {
    if (!_diningTimeTitleLb) {
        _diningTimeTitleLb = [UILabel new];
        NSString *yuyeuTimeStr = NSLocalizedString(Order_TimeAppointment, @"预约时间");
        _diningTimeTitleLb.text = yuyeuTimeStr;
        _diningTimeTitleLb.textAlignment = NSTextAlignmentCenter;
        _diningTimeTitleLb.font = [UIFont systemFontOfSize:kFont14];
        _diningTimeTitleLb.numberOfLines = 0;
    }
    return _diningTimeTitleLb;
}

- (UILabel *)diningTimeLbBgView {
    if (!_diningTimeLbBgView) {
        _diningTimeLbBgView = [[UILabel alloc] init];
    }
    return _diningTimeLbBgView;
}


- (UILabel *)diningTimeLb {
    if (!_diningTimeLb) {
        _diningTimeLb = [UILabel new];
        _diningTimeLb.numberOfLines = 0;
//        _diningTimeLb.font = [UIFont systemFontOfSize:14.0*kScaleFit];
        _diningTimeLb.textAlignment = NSTextAlignmentCenter;
        _diningTimeLb.font = [UIFont systemFontOfSize:13.0]; //
        _diningTimeLb.textColor = kLightGrayTextColor;
    }
    return _diningTimeLb;
}

- (UILabel *)yuyueNumTitleLb {
    if (!_yuyueNumTitleLb) {
        _yuyueNumTitleLb = [UILabel new];
        NSString *yuyueHaoStr = NSLocalizedString(Order_MyNumber, @"我的预约号");
        _yuyueNumTitleLb.text = yuyueHaoStr;
        _yuyueNumTitleLb.textAlignment = NSTextAlignmentCenter;
        _yuyueNumTitleLb.font = [UIFont systemFontOfSize:kFont14];
        _yuyueNumTitleLb.numberOfLines = 0; //fix 国际化适配
    }
    return _yuyueNumTitleLb;
}
- (UILabel *)yuyueNumLb {
    if (!_yuyueNumLb) {
        _yuyueNumLb = [UILabel new];
        _yuyueNumLb.font = [UIFont systemFontOfSize:30*kScaleFit];
        _yuyueNumLb.textColor = kMainRedColor;
        _yuyueNumLb.textAlignment = NSTextAlignmentCenter;
    }
    return _yuyueNumLb;
}
- (UILabel *)renshuTitleLb {
    if (!_renshuTitleLb) {
        _renshuTitleLb = [UILabel new];
        _renshuTitleLb.textAlignment = NSTextAlignmentCenter;
        _renshuTitleLb.font = [UIFont systemFontOfSize:kFont14];
        //国际化
        NSString *renshuStr = NSLocalizedString(Order_Number, @"人数");
        _renshuTitleLb.text = renshuStr;
    }
    return _renshuTitleLb;
}
- (UILabel *)renshuLb {
    if (!_renshuLb) {
        _renshuLb = [UILabel new];
        _renshuLb.textAlignment = NSTextAlignmentCenter;
        _renshuLb.text = @"10";
        _renshuLb.font = [UIFont systemFontOfSize:20.0];
    }
    return _renshuLb;
}


//!!!!:div3
//==========下============
- (UIView *)div3 {
    if (!_div3) {
        _div3 = [UIView new];
        _div3.backgroundColor = [UIColor whiteColor];
    }
    return _div3;
}
- (UIButton *)anNiuBtn {
    if (!_anNiuBtn) {
        _anNiuBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_anNiuBtn setTitle:@"预约点餐" forState:(UIControlStateNormal)];
        _anNiuBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        
        [_anNiuBtn setTitleColor:kMainRedColor forState:(UIControlStateNormal)];
        _anNiuBtn.layer.masksToBounds = YES;
        _anNiuBtn.layer.cornerRadius = 5;
        _anNiuBtn.layer.borderColor = kMainRedColor.CGColor;
        _anNiuBtn.layer.borderWidth = 1.0;
        
        //关闭响应者链 点击点击按就是点击cell
        _anNiuBtn.userInteractionEnabled = NO;
    }
    return _anNiuBtn;
}


#pragma mark - 赋值
- (void)setModel:(YuYueOrderModel *)model {
    _model = model;
    
    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:model.thumbnail]];
    self.dianNameLb.text = model.name;
    self.createTimeLb.text = model.createtimeStr;
    
    //div2
    self.diningTimeLb.text = model.diningTimeStr;
    self.yuyueNumLb.text = [NSString stringWithFormat:@"%@",model.number];
    self.renshuLb.text = [NSString stringWithFormat:@"%@",model.peopleNumber];
    
    //div3
//11-12更新：根据
//    model.userOrderId //判断显示"预约点餐“ 还是查看详情。
    
    NSString *lookDetailStr = NSLocalizedString(Order_LookDetail, @"查看详情");
    NSString *yuyueDianCan  = NSLocalizedString(Home_YuYueDianCan, @"预约点餐");
    if ([NSString isEmptyString:model.userOrderId]) {
        [self.anNiuBtn setTitle:yuyueDianCan forState:(UIControlStateNormal)];
    }else {
        [self.anNiuBtn setTitle:lookDetailStr forState:(UIControlStateNormal)];
    }
    
//    if ([model.status isEqualToNumber:@0]) { // 待用餐 显示  @“预约点餐按钮”
//        [self.anNiuBtn setTitle:@"预约点餐" forState:(UIControlStateNormal)];
//    }else { //@"1 已经完成"
//        [self.anNiuBtn setTitle:@"查看详情" forState:(UIControlStateNormal)];
//    }
}

@end
